Welcome back to our costs.
So this session provides you with a quick overview over the data type tuple.
So a tuple you can store many other objects and therefore tuple is also a sequence of many elements
like for example a list.
So to put this in some ways comparable to our list.
However a tuple has much less functionality than a list and therefore we don't use it as often and as
much as we we the list.
But however there are many situations in Python programming where we we get in touch with tuples so
we should at least know the basic functionality and the basics of tuples.
All right.
So let's start creating our first tuple by creating of our able a A equals and pattern to this one two
three
so a s a tuple containing three elements One two and three and we can also create a tuple without use.
So if you just type B equals one two three is separated by Hamas.
This also works and the same s with lists you can put the many different data types and many different
elements into a tuple.
So for example let's create a tuple C C equals 1 2.5 and talk.
So we have feared integer we have a float and we have a string object so that's no problem it works
like in the list and we can also index the tuple like in the list let's say if you want to get the second
element of our tuple at an exposition one I would expect to get them to
exactly.
And also slicing works the same way as both lists if you want to slice tuple C from the second element.
So beginning from index position 1 until the very last element.
Then this also works.
And also if we multiply a tuple with an integer eight times two.
So we have the same behavior as the lists that actually we create copies of A and concatenate the copies
so we don't have element y's multiplication as one one might expect actually.
And also we can concatenate to 2:00 so we have total a and we f and number tuple with the elements for
5 6.
Then we can just concatenate it to a large Topo and the most important difference between a total and
a list.
This that the list is mutable so you can change elements of a list.
And this is not possible with a total so if you want to change the first element of tuple a let's say
change it to 100 then Python drops us an error message so tuple object does not some plot item assignment.
So our total is immutable and this heavily restricts the functionality of tuples.
So there are no not too much cases where we need tuples.
However in python there are many functions that return multiple values.
And so if a function returns multiple values it's in most cases it returns that via returning a tuple
of these multiple elements so let's let's have an example here.
So let's create the list with five elements.
List El.
And now we apply the function enumerate on Al.
So what enumerate actually does the stocks index and value pass of list.
L So actually it stops.
So we have fear element one at an exposition from 0 and enumerate stores 0 1.
So in exposition 0 now.
So the element 1 then at an exposition 1 this element to enumerate creates and starts the index while
you pass off a list.
So and if I run now the enumerate function pacing does not return the index so well you pass.
So it's only creating a so-called iterator.
So what is an Iterator iterator we will learn later more about iterate us but to put it in easy words
by running enumerate we are telling python.
So please python and make yourself ready to give me the index value you pass at the time that I need
it.
So but now at this step it does not create and store the past it only creates the value you pass if
I explicitly call it in another step and I can do it here.
In this case by applying the list function on our iterator object.
So now let's apply the list function on our enumerate object and assign the arrival n
and now we get a list with the five elements and these elements tuples and these are tuples of indexed
value pass.
So we have that indexed position 0 the element 1 We are fed index position 1 The Element 2 and so on
at the index position far the element 5 and this is one example of where we get in touch with tuples.
So even if you if you don't want to get in touch with two bits there are many functions that return
to both actually and then let's slice them the first tuple here.
Let's say we take the first tuple at an exposition 0 and call it first tuple.
So now we have one tuple of with the elements 0 and 1 and then what we can do we can unpack a tuple
by saying Okay the first element I call index and the second element I will call a value so index comma
value is equal to first tuple and now if you print index and value we get the unpacked three elements
of the tuple.
So index is 0 and values 1 in but we can also do we can let's call again our list l we can transform
a list into a tuple so that's that's also possible.
So now we have a tuple with five elements.
All right.
So this was a quick overview of our tuples.
Hope you enjoyed it.
And yeah we see us in the next session about sets.